<?php
declare(strict_types=1);
/**
 * Created by PhpStorm
 * User: Administrator
 * Date: 2023/8/4
 * Time: 16:22:10
 */

namespace Service\CommodityService;

use App\Model\BbcModel\GoodsManagerTags;
use App\Model\BbcModel\GoodsManagerTagGroup;
use App\Model\BbcModel\GoodsTagsGroup;
use Service\Code\Normal;
use App\Model\BbcModel\Goods;
use Service\BaseService;

class GoodsManagerTagService extends BaseService
{

    public function makeWhere($param){
        $where = [
            ['sid','=',$param['sid']],
            ['cid','=',$param['cid']],
            ['df','=',0],
            ['type','=',$param['type']],
        ];

        if(isset($param['name']) && $param['name'] != ''){
            $where[] = ['name','like',"%{$param['name']}%"];
        }

        return $where;
    }

    public function goodsTagList($param) : array
    {
        if(!isset($param['type'])){
            return Normal::returnCode(Normal::FAIL,'缺少标签类型参数');
        }
        $limit = $param['limit'] ?? 10;
        $page = $param['page'] ?? 1;
        $where = $this->makeWhere($param);
        $data_list = GoodsManagerTags::instance()
            ->where($where)
            ->orderByRaw('sort desc,id desc')
            ->paginate($limit, ['id','name','type','img','desc','sort','md','cd'], 'page', $page);
        $list = $data_list->items();
        $total = $data_list->total();

        foreach ($list as &$v){
            if($v['cd']){
                $v['cd'] = date('Y-m-d H:i:s');
            }
            if($v['md']){
                $v['md'] = date('Y-m-d H:i:s');
            }
            $v['goods_num'] = Goods::instance()
                ->where(['cid'=>$param['cid'],'df'=>0])
                ->where(function($query) use ($v){
                    if($v['type'] == 0){
                        $query->whereRaw("find_in_set({$v['id']},supply_tag)");
                    }else{
                        $query->whereRaw("find_in_set({$v['id']},tag)");
                    }

                })->count();
        }
        return Normal::returnCode(Normal::SUC,'请求成功',['list'=>$list,'total'=>$total]);
    }


    /**
     * 新增标签
     * @param $param
     * @return array
     */
    public function addGoodsTag($param){

        if(!isset($param['type']) && $param['type'] == ''){
            return Normal::returnCode(Normal::FAIL,'请选择标签类型');
        }

        if(!isset($param['name']) || $param['name'] == ''){
            return Normal::returnCode(Normal::FAIL,'标签名称不能为空');
        }

        if($param['type'] == 0 && $param['img']){ //小程序标签
            return Normal::returnCode(Normal::FAIL,'小程序标签样式不能为空');
        }


        $max_sort = GoodsManagerTags::instance()
            ->where(['cid'=>$param['cid'],'df'=>0])
            ->max('sort');

        $res = GoodsManagerTags::instance()
            ->insert([
                'cid'      => $param['cid'],
                'sid'      => $param['sid'],
                'name'     => trim($param['name']),
                'type'     => $param['type'],
                'img'      => $param['img'] ?? '',
                'desc'     => $param['desc'] ?? '',
                'sort'     => $param['sort'] ?? $max_sort + 1,
            ]);

        if(!$res){
            return Normal::returnCode(Normal::FAIL,'新增失败');
        }
        return Normal::returnCode(Normal::SUC,'新增成功');
    }

    /**
     * 编辑标签
     * @param $param
     * @return array
     */
    public function editGoodsTag($param){
        if(!isset($param['id'])){
            return Normal::returnCode(Normal::FAIL,'标签ID不能为空');
        }

        if(!isset($param['name']) || $param['name'] == ''){
            return Normal::returnCode(Normal::FAIL,'标签名称不能为空');
        }

        $info = GoodsManagerTags::instance()
            ->where(['cid'=>$param['cid'],'id'=>$param['id'],'df'=>0])
            ->first();
        if(!$info){
            return Normal::returnCode(Normal::FAIL,'数据不存在');
        }
        if($info['type'] == 0 && $param['img']){ //小程序标签
            return Normal::returnCode(Normal::FAIL,'小程序标签样式不能为空');
        }
        $res = GoodsManagerTags::instance()
            ->where(['id'=>$info['id']])
            ->update([
                'name'     => trim($param['name']),
                'type'     => $param['type'],
                'img'      => $param['img'] ?? '',
                'desc'     => $param['desc'] ?? '',
                'sort'     => $param['sort'] ?? $info['sort'],
            ]);

        if(!$res){
            return Normal::returnCode(Normal::FAIL,'编辑失败');
        }
        return Normal::returnCode(Normal::SUC,'编辑成功');

    }

    public function delGoodsTags($param) : array{
        if(!isset($param['id']) || !$param['id']){
            return Normal::returnCode(Normal::FAIL,'缺少标签ID');
        }

        $has = GoodsManagerTags::instance()
            ->where(['cid'=>$param['cid'],'id'=>$param['id'],'df'=>0])
            ->exists();
        if(!$has){
            return Normal::returnCode(Normal::FAIL,'商品标签不存在');
        }

        $res = GoodsManagerTags::instance()
            ->where(['id'=>$param['id']])
            ->update(['df'=>1,'md'=>time()]);

        if(!$res){
            return Normal::returnCode(Normal::FAIL,'删除失败');
        }

        return Normal::returnCode(Normal::SUC,'删除成功');
    }

    public function moveUpDown($param){
        if(!isset($param['id']) || !$param['id']){
            return Normal::returnCode(Normal::FAIL,'缺少标签ID');
        }
        if(!isset($param['type']) || !$param['type']){
            return Normal::returnCode(Normal::FAIL,'缺少移动类型参数');
        }
        $table = GoodsManagerTags::instance();
        $where = [
            ['cid','=',$param['cid']],
            ['sid','=',$param['sid']],
            ['df','=',0],
        ];
        return (new \Service\MoveUpDownService\MoveUpDownService())->moveUpDown($param['id'],$param['type'],$table,$where,'sort');
    }

    public function setTop($param) : array{
        if(!isset($param['id']) || !$param['id']){
            return Normal::returnCode(Normal::FAIL,'缺少品牌ID');
        }

        $table = GoodsManagerTags::instance();
        $where = [
            ['cid','=',$param['cid']],
            ['sid','=',$param['sid']],
            ['df','=',0],
        ];
        $res = (new \Service\MoveUpDownService\MoveUpDownService())->setTop($param['id'],$table,$where,'sort',$param['type']);
        if($res['code'] != Normal::SUC){
            return Normal::returnCode(Normal::FAIL,$res['msg']);
        }
        return Normal::returnCode(Normal::SUC,'操作成功');
    }
}